import { action, observable } from 'mobx';
import * as Api from '../../api/plan/planViewApi';
import { Browser, BrowserHrm, ListViewPage } from 'weaver-mobile-page';
import { Toast, Tools, Bubble } from 'weaver-mobile';

const getLabel = Tools.getLabel;

/**
 * 审核
 */
export default class PlanAuditStore {

    @observable list = new ListViewPage.Store({ route: '/planHome/planAudit' });
    @observable subBrowser = new Browser.Store({ route: '/planHome/planAudit/condition/subBrowser' });
    @observable deptBrowser = new Browser.Store({ route: '/planHome/planAudit/condition/deptBrowser' });
    @observable browserHrm = new BrowserHrm.Store({ route: '/planHome/planAudit/condition/browserHrm' });
    @observable subBrowserValue = [];
    @observable deptBrowserValue = [];
    @observable browserHrmValue = [];

    @observable queryParams = { //查询条件
        searchTitleValue: '', //标题
        searchDateType: '', //类型
        searchHumSelectValue: '', //人员
        searchIncludeSubSelectValue: "3", //是否包含分部，默认含子分部
        searchSubSelectValue: '', //分部
        searchIncludeDepSelectValue: "3", //是否包含分部，默认含子部门
        searchDepSelectValue: '', //部门
    };
    @observable planTypeData = [
        { key: '1', title: getLabel(20617, '月报'), showname: getLabel(20617, '月报') },
        { key: '2', title: getLabel(20619, '周报'), showname: getLabel(20619, '周报') },
    ]
    @observable includesubData = [
        { key: "3", showname: getLabel('508780', '含子分部') },
        { key: "1", showname: getLabel('18919', '仅本分部') },
        { key: "2", showname: getLabel('18920', '仅子分部') },
    ];
    @observable includedeptData = [
        { key: "3", showname: getLabel('508784', '含子部门') },
        { key: "1", showname: getLabel('18916', '仅本部门') },
        { key: "2", showname: getLabel('18917', '仅子部门') },
    ];
    @observable tabs = [{"isSearch":false,"key":"2","name": getLabel(18770, "按组织结构"),"selected":false,"showOrder":0}
    ,{"dataParams":{"list":"1"},"isSearch":true,"key":"1","name":getLabel(130619, '按列表'),"selected":false,"showOrder":0}];


    @action("初始化操作")
    doInit = () => {
        this.getTableDatas();
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("重置条件")
    resetCondition = () => {
        this.queryParams = { //查询条件
            ...this.queryParams,
            searchTitleValue: '', //标题
            searchDateType: '', //类型
            searchHumSelectValue: '', //人员
            searchIncludeSubSelectValue: "3", //是否包含分部，默认含子分部
            searchSubSelectValue: '', //分部
            searchIncludeDepSelectValue: "3", //是否包含分部，默认含子部门
            searchDepSelectValue: '', //部门
        };
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.browserHrmValue = [];
    }

    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    @action("渲染table数据")
    getTableDatas = () => {
        this.list.clearData();
        Api.planAuditList(this.queryParams).then(action(res => {
            if (res.api_status) {
                const { datas: dataKey } = res;
                this.list.getData({ dataKey });
            } else {
                Bubble.error(getLabel('508818', '接口调用失败！'), 3, null, null, null, false);
            }
        }));
    };


    @action("单个审核")
    auditSingle = (params) => {
        //planid  operation
        Api.auditSingle(params).then(action(res => {
            if (res.api_status) {
                this.getTableDatas()
                if (window.refreshPlanAuditNum && typeof window.refreshPlanAuditNum === "function") {
                    window.refreshPlanAuditNum();
                }
            } else {
                Bubble.error(getLabel('508818', '接口调用失败！'), 3, null, null, null, false);
            }

        }));
    };

    @action("批量审核")
    auditBatch = (params) => {
        Api.auditBatch(params).then(action(res => {
            if (res.api_status) {
                this.getTableDatas()
                if (window.refreshPlanAuditNum && typeof window.refreshPlanAuditNum === "function") {
                    window.refreshPlanAuditNum();
                }
            } else {
                Bubble.error(getLabel('508818', '接口调用失败！'), 3, null, null, null, false);
            }

        }));
    };


}